<template>
  <div class="unitManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.unitName" placeholder="名称" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">新增</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table border v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column show-overflow-tooltip align="center" min-width="50" type="selection"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="unitCode" label="编码"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="unitName" label="名称"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="createdBy" label="创建人"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="160" prop="createdTime" label="创建时间"></el-table-column>
      <el-table-column align="center" width="100" label="操作">
        <template #default="{ row }">
          <el-tooltip effect="dark" content="编辑当前行" placement="top">
            <el-button icon="el-icon-edit" type="primary" size="mini" circle @click="handleEdit(row)"></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除当前行" placement="top">
            <el-button icon="el-icon-delete" type="danger" size="mini" circle @click="handleDelete(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>

    <edit ref="edit" @fetch-data="fetchData"></edit>
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/basics/unit'
  import Edit from './components/Edit'

  export default {
    name: 'Unit',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          unitName: undefined,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row) {
        if (row.unitId) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await doDelete(row.unitId).then((res) => {
              if (res.success) {
                this.$baseMessage(res.msg, 'success')
                this.fetchData()
              } else {
                this.$baseMessage(res.msg, 'error')
              }
            })
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.total
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
